// 1 防抖
export function debounce(func, ms = 1000) {
    let timer;
    return function (...args) {
        if (timer) {
            clearTimeout(timer)
        }
        timer = setTimeout(() => {
            func.apply(this, args)
        }, ms)
    }
}
// 2 节流
export function throttle(func, ms = 1000) {
    let canRun = true
    return function (...args) {
        if (!canRun) return
        canRun = false
        setTimeout(() => {
            func.apply(this, args)
            canRun = true
        }, ms)
    }
}

// 3 格式化时间
export function upTime(format,time) {
    if(!time) {
        return ''
    }
    // let format = "%Y-%M-%D %h:%m:%s"
    format = format ? format: "%Y-%M-%D"
    time = time.toString().length == 10 ? time*1000:time
    let dataTime = new Date(time)
    let year = dataTime.getFullYear()
    let month = (dataTime.getMonth() + 1).toString().padStart(2, 0)
    let day = dataTime.getDate().toString().padStart(2, 0)
    let Hours = dataTime.getHours().toString().padStart(2, 0)
    let Minutes = dataTime.getMinutes().toString().padStart(2, 0)
    let Seconds = dataTime.getSeconds().toString().padStart(2, 0)
    format = format.replace(/(%Y|%M|%D|%h|%m|%s)/g, (n) => {
        return {
            '%Y': year, '%M': month, '%D': day, '%h': Hours, '%m': Minutes, '%s': Seconds,
        }[n]
    })
    return format
}
/**
 *
 * @param {*} option  本地缓存键 key  本地缓存值 value
 * @description value设置则为更新，不设置则为获取
 *
 */
export function storage(key, value) {
    if (value) {
      my.setStorageSync({
        key: key,
        data: value
      });
    }
    else {
        const data = my.getStorageSync({ key: key})
      //   console.log("======getStorageSync缓存获取结果=======\n", key,data.data);
        return data.data;
    }
  }